﻿using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace 连续播放网络音频
{
    class Program
    {
        static void Main(string[] args)
        {
            //var url_1 = "https://yiqihebo.oss-cn-beijing.aliyuncs.com/section/1.mp3?OSSAccessKeyId=LTAICK0n3B2bWw10&Expires=1551347069&Signature=C1%2Bq1WLrAMRWU1VBzRvyEElJnVU%3D";
            //var url_2 = "https://yiqihebo.oss-cn-beijing.aliyuncs.com/section/2.mp3?OSSAccessKeyId=LTAICK0n3B2bWw10&Expires=1551347111&Signature=txuz1fqlYah7Aqgql5WowQ%2FPUDM%3D";
            //var mf_1 = new MediaFoundationReader(url_1);
            //var mf_2 = new MediaFoundationReader(url_2);


            //using (var wo = new WaveOutEvent())
            //{
            //    wo.Init(mf_1);
            //    wo.Play();
                
            //    while (wo.PlaybackState == PlaybackState.Playing)
            //    {
            //        Thread.Sleep(10);
            //    }
            //}

            //using (var wo = new WaveOutEvent())
            //{
            //    wo.Init(mf_2);
            //    wo.Play();


            //    while (wo.PlaybackState == PlaybackState.Playing)
            //    {
            //        Thread.Sleep(10);
            //    }
            //}

            Play("https://yiqihebo.oss-cn-beijing.aliyuncs.com/section/1.mp3?OSSAccessKeyId=LTAICK0n3B2bWw10&Expires=1551347069&Signature=C1%2Bq1WLrAMRWU1VBzRvyEElJnVU%3D");

            while (true)
            {
                Thread.Sleep(1000);
            }
        }

        private static void Play(string url)
        {
            var mf = new MediaFoundationReader(url);
            using (var wo = new WaveOutEvent())
            {
                wo.PlaybackStopped += Wo_PlaybackStopped;
                wo.Init(mf);
                wo.Play();
                while (wo.PlaybackState == PlaybackState.Playing)
                {
                    Thread.Sleep(1);
                }
            }
        }

        private static void Wo_PlaybackStopped(object sender, StoppedEventArgs e)
        {
            Play("https://yiqihebo.oss-cn-beijing.aliyuncs.com/section/2.mp3?OSSAccessKeyId=LTAICK0n3B2bWw10&Expires=1551347111&Signature=txuz1fqlYah7Aqgql5WowQ%2FPUDM%3D");
        }
    }
}
